﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ShopManagement.Dao;
using ShopManagement.Common;

namespace ShopManagement.Forms
{
    public partial class OrderForm : Form
    {

        #region Attributes
        private SmDao _myDao;
        private Order _myOrder;
        //0: new; 1: update
        private int _mode;
        #endregion

        public OrderForm()
        {
            InitializeComponent();
        }

        public OrderForm(SmDao inputDao)
        {
            InitializeComponent();
            _mode = 0;
            _myDao = inputDao;
        }

        public OrderForm(SmDao inputDao, Order myOrder)
        {
            InitializeComponent();
            _myDao = inputDao;
            _mode = 1;
            _myOrder = myOrder;
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            int status = 0;
            int changedQuantity = 0;
            int quantity;
            if(!(int.TryParse(txtQuantity.Text,out quantity) && quantity > 0))
            {
                status = 1;
                MessageBox.Show("Số Lượng Không Hợp Lệ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            Product myPd = new Product();
            myPd = _myDao.GetAProduct(txtPdCode.Text);
            if (_mode == 1)
            {
                changedQuantity = quantity - _myOrder.Quantity;
            }
            else
            {
                changedQuantity = quantity;
            }
            if((myPd.Sold + changedQuantity) > myPd.Quantity)
            {
                status = 1;
                MessageBox.Show(String.Format("Không Còn Đủ Hảng. Chỉ còn {0} Cái", myPd.Quantity - myPd.Sold),
                                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if(status != 1)
            {
                if (_mode == 0)
                {
                    Order myOrder = new Order();
                    myOrder.CustomerName = txtCustomerName.Text;
                    myOrder.ProductId = txtPdCode.Text;
                    myOrder.Quantity = quantity;
                    myOrder.StartDate = dtPickerStart.Value;
                    myOrder.EndDate = dtPickerEnd.Value;
                    myOrder.Status = cbStatus.SelectedItem.ToString();
                    myOrder.Detail = txtDetail.Text;
                    myOrder.Profit = myPd.Price - myPd.NetValue;
                    _myDao.InsertOrder(myOrder);
                    myPd.Sold = myPd.Sold + quantity;
                    _myDao.UpdateProduct(myPd); 
                }
                if(_mode == 1)
                {
                    _myOrder.CustomerName = txtCustomerName.Text;
                    _myOrder.ProductId = txtPdCode.Text;
                    _myOrder.Quantity = quantity;
                    _myOrder.StartDate = DateTime.Parse(txtStartDate.Text);
                    _myOrder.EndDate = DateTime.Parse(txtEndDate.Text);
                    _myOrder.Detail = txtDetail.Text;
                    if(_myOrder.Status != cbStatus.SelectedItem.ToString())
                    {
                        if(_myOrder.Status == Constants.OrderStatus.New)
                        {
                            if(cbStatus.SelectedItem.ToString() == Constants.OrderStatus.Cancel)
                            {
                                myPd.Sold = myPd.Sold + changedQuantity;
                                _myDao.UpdateProduct(myPd);
                            }
                        }
                        if(_myOrder.Status == Constants.OrderStatus.Done)
                        {
                            if(cbStatus.SelectedItem.ToString() == Constants.OrderStatus.Cancel)
                            {
                                myPd.Sold = myPd.Sold + changedQuantity;
                                _myDao.UpdateProduct(myPd);
                            }
                        }
                        if(_myOrder.Status == Constants.OrderStatus.Cancel)
                        {
                            myPd.Sold = myPd.Sold + changedQuantity;
                            _myDao.UpdateProduct(myPd);
                        }
                    }
                    else
                    {
                        if(_myOrder.Status != Constants.OrderStatus.Cancel)
                        {
                            myPd.Sold += changedQuantity;
                            _myDao.UpdateProduct(myPd);
                        }
                    }
                    _myOrder.Status = cbStatus.SelectedItem.ToString();
                    _myOrder.Profit = myPd.Price - myPd.NetValue;
                    _myDao.UpdateOrder(_myOrder);
                }
                Close();
            }
        }

        private void txtPdCode_Leave(object sender, EventArgs e)
        {
            string pdCode = txtPdCode.Text;
            Product myPd = new Product();
            myPd = _myDao.GetAProduct(pdCode);
            if (String.IsNullOrEmpty(myPd.ProductCode))
            {
                MessageBox.Show("Mã Sản Phẩm Này Không Tồn Tại", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPdCode.Text = "";
            }
            else
            {
                pbPreview.ImageLocation = myPd.Image;
                pbPreview.SizeMode = PictureBoxSizeMode.StretchImage;
                pbPreview.Show();
            }
        }

        private void OrderForm_Load(object sender, EventArgs e)
        {
            if (_mode == 0)
            {
                txtStartDate.Text = dtPickerStart.Value.ToString(Constants.FullYearToDate);
                txtEndDate.Text = dtPickerEnd.Value.ToString(Constants.FullYearToDate);
                cbStatus.SelectedIndex = 0; 
            }
            if(_mode == 1)
            {
                txtCustomerName.Text = _myOrder.CustomerName;
                txtPdCode.Text = _myOrder.ProductId;
                txtStartDate.Text = _myOrder.StartDate.ToString(Constants.FullYearToDate);
                txtEndDate.Text = _myOrder.EndDate.ToString(Constants.FullYearToDate);
                txtQuantity.Text = _myOrder.Quantity.ToString();
                if(_myOrder.Status == Constants.OrderStatus.New)
                {
                    cbStatus.SelectedIndex = 0;
                }
                else if(_myOrder.Status == Constants.OrderStatus.Done)
                {
                    cbStatus.SelectedIndex = 1;
                }
                else
                {
                    cbStatus.SelectedIndex = 2;
                }
                txtDetail.Text = _myOrder.Detail;
                Product myPd = new Product();
                myPd = _myDao.GetAProduct(_myOrder.ProductId);
                pbPreview.ImageLocation = myPd.Image;
                pbPreview.SizeMode = PictureBoxSizeMode.StretchImage;
                pbPreview.Show();
            }
        }

        private void dtPickerStart_ValueChanged(object sender, EventArgs e)
        {
            txtStartDate.Text = dtPickerStart.Value.ToString(Constants.FullYearToDate);
        }

        private void dtPickerEnd_ValueChanged(object sender, EventArgs e)
        {
            txtEndDate.Text = dtPickerEnd.Value.ToString(Constants.FullYearToDate);
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

    }
}
